
import { BaseValue } from "../data/BaseValue";

const { ccclass, property } = cc._decorator;

@ccclass
export class AudioManager {

    /**
     * 背景音乐
     * @param name 音乐名
     * 
     */
    public static playMusic(name: string, volume: number) {
        const path = `music/back/${name}`;
        cc.loader.loadRes(path, cc.AudioClip, (err: any, clip: cc.AudioClip) => {
            if (err) {
                console.warn(err)
                return;
            }
            cc.audioEngine.play(clip, true, volume);
        })
    }


    /**
     * 音效
     * @param name 音乐名
     * 
     */
    public static playEffect(name: string) {
        const path = `music/effects/${name}`;
        cc.loader.loadRes(path, cc.AudioClip, (err: any, clip: cc.AudioClip) => {
            if (err) {
                console.warn(err)
                return;
            }
            cc.audioEngine.play(clip, false, 1);
        })
    }

    /**
     * 播放网络音乐
     * @param url 
     * @param volume 
     */
    public static playMusicFromLink(url: string, volume: number) {
        let link = BaseValue.BASE_OSS_URL + url;
        cc.loader.load(link, async (err: any, clip: cc.AudioClip) => {
            if (err) {
                console.warn(err.message || err);
                return;
            }
            cc.audioEngine.play(clip, true, volume)
        })
    }

    /**
     * 加载网络图片, 更改节点图片
     * @param node 
     * @param url 
     */
    public static loadImageFromLink(node: cc.Sprite, url: string) {
        let link = BaseValue.BASE_OSS_URL + url;
        cc.loader.load(link, (err: any, image: cc.Texture2D) => {
            if (err) {
                console.warn(err.message || err);
                return;
            }
            node.spriteFrame = new cc.SpriteFrame(image);
        })
    }

}
